﻿using System.Collections.ObjectModel;
using System.Linq;
using AchievementSample.Extensions;
using Achievements;

namespace AchievementSample.ViewModels
{
    public class MainViewModel : ViewModelBase
    {
        private const string UnlockedAchievementsPropertyString = "UnlockedAchievements";
        private const string LockedAchievementsPropertyString = "LockedAchievements";
        private const string AllAchievementsPropertyString = "AllAchievements";
        private ObservableCollection<Achievement> _allAchievements;
        private ObservableCollection<Achievement> _lockedAchievements;

        private ObservableCollection<Achievement> _unlockedAchievements;

        public MainViewModel()
        {
            AllAchievements = AppAchievements.Instance.AllAchievements;
            AppAchievements.Instance.AllAchievements.CollectionChanged += (s, e) =>
                                                                              {
                                                                                  RaisePropertyChanged(
                                                                                      AllAchievementsPropertyString);
                                                                                  RaisePropertyChanged(
                                                                                      LockedAchievementsPropertyString);
                                                                                  RaisePropertyChanged(
                                                                                      UnlockedAchievementsPropertyString);
                                                                              };
            foreach (Achievement achievement in AllAchievements)
            {
                achievement.PropertyChanged += (s, e) =>
                                                   {
                                                       if (e.PropertyName.Equals(Achievement.IsUnlockedPropertyString))
                                                       {
                                                           var achieve = ((Achievement) s);
                                                           if (achieve.IsUnlocked)
                                                           {
                                                               UnlockedAchievements.Add(achieve);
                                                               LockedAchievements.Remove(achieve);
                                                           }
                                                       }
                                                   };
            }
        }

        public ObservableCollection<Achievement> UnlockedAchievements
        {
            get
            {
                return _unlockedAchievements ??
                       (_unlockedAchievements =
                        AllAchievements == null
                            ? null
                            : AllAchievements.Where(mem => mem.IsUnlocked).ToObservableCollection());
            }
            set
            {
                if (_unlockedAchievements != value)
                {
                    _unlockedAchievements = value;
                    RaisePropertyChanged(UnlockedAchievementsPropertyString);
                }
            }
        }

        public ObservableCollection<Achievement> LockedAchievements
        {
            get
            {
                return _lockedAchievements ??
                       (_lockedAchievements =
                        AllAchievements == null
                            ? null
                            : AllAchievements.Where(mem => !mem.IsUnlocked).ToObservableCollection());
            }
            set
            {
                if (_lockedAchievements != value)
                {
                    _lockedAchievements = value;
                    RaisePropertyChanged(LockedAchievementsPropertyString);
                }
            }
        }

        public ObservableCollection<Achievement> AllAchievements
        {
            get { return _allAchievements; }
            set
            {
                if (_allAchievements != value)
                {
                    _allAchievements = value;
                    RaisePropertyChanged(AllAchievementsPropertyString);
                    RaisePropertyChanged(LockedAchievementsPropertyString);
                    RaisePropertyChanged(UnlockedAchievementsPropertyString);
                }
            }
        }
    }
}